using System;
using System.Windows.Forms;

using FridayShopping.DataModel;

namespace FridayShopping.UI
{
	/// <summary>
	/// Manages the list of items that are shown in a listview located on a form
	/// </summary>
	public class ListFormManager
	{
		protected IListViewForm		form;
		protected ListView          listView;
        protected IItemList			list;

		public ListFormManager(IListViewForm form, ListView listView, IItemList list)
		{
			this.form = form;
			this.listView = listView;
            this.list = list;
		}

		public ListFormManager(IListViewForm form, ListView listView)
		{
			this.form = form;
			this.listView = listView;
		}

        public void FillListView() 
		{

            listView.Items.Clear();

			if (NeedToSort)
			{
				list.List.Sort();
			}

            foreach (ItemBase item in list.List) 
            {
				if (ShouldAddToListView(item))
				{
					AddListViewItem(item);
				}
            }
        }

		virtual protected bool NeedToSort
		{
			get
			{
				return true;
			}
		}

		virtual protected bool ShouldAddToListView(ItemBase item) 
		{
			return true;
		}

        protected void AddListViewItem(ItemBase item)
        {
            listView.Items.Add(CreateListViewItem(item));
        }

        virtual protected ListViewItem CreateListViewItem(ItemBase item) 
        {
            string[] subitems = {
                                    item.Key.ToString(),
                                    item.Name
                                };
            return new ListViewItem(subitems);
        }

        public Key GetItemKey(ListViewItem item) 
        {
            Key key = new Key(Convert.ToInt32(item.SubItems[0].Text));
            return key;
        }

        protected ItemBase GetItem(ListViewItem listItem) 
        {
            Key key = GetItemKey(listItem);
            return list.GetItem(key);            
        }

        virtual protected void DeleteItem(ListViewItem item)
        {
            Key key = GetItemKey(item);
            list.Delete(key);
            listView.Items.Remove(item);
        }

        virtual protected void AddItem(ItemBase item) 
        {
            list.Add(item);
            AddListViewItem(item);
        }

        virtual protected void RefreshIndexedItem(int index, ItemBase item) 
        {
			if (listView.Items.Count > index) 
			{
				listView.Items.RemoveAt(index);
				listView.Items.Insert(index, CreateListViewItem(item));
				listView.Items[index].Focused = true;
				listView.Items[index].Selected = true;
			}
        }

		protected void RefreshCurrentItem(ItemBase item)
		{
			int index = listView.FocusedItem.Index;
			RefreshIndexedItem(index, item);
		}

        virtual protected void UpItem(ListViewItem listItem) 
        {
            ItemBase item = GetItem(listItem);
			if (item == null) 
			{
				return;
			}
            list.MoveUp(item);
            int index = listItem.Index;
            listView.Items.RemoveAt(index);
            listView.Items.Insert(index - 1, listItem);
            listView.Items[index-1].Focused = true;
            listView.Items[index-1].Selected = true;
        }

        virtual protected void DownItem(ListViewItem listItem) 
        {
            ItemBase item = GetItem(listItem);
			if (item == null) 
			{
				return;
			}
            list.MoveDown(item);
            int index = listItem.Index;
            listView.Items.RemoveAt(index);
            listView.Items.Insert(index + 1, listItem);
            listView.Items[index + 1].Focused = true;
            listView.Items[index + 1].Selected = true;
        }

		virtual public void EnableDisableItems()
		{
			bool toEnable = false;
			if (listView.SelectedIndices.Count > 0) 
			{
				toEnable = listView.FocusedItem.Selected;
			}
			if (form.DeleteButton != null)
			{
				form.DeleteButton.Enabled = toEnable;
			}
			if (form.EditButton != null) 
			{
				form.EditButton.Enabled = toEnable;
			}
			if (form.UpButton != null && form.DownButton != null) 
			{
				if (toEnable) 
				{
					int index = listView.FocusedItem.Index;
					if (index + 1 < listView.Items.Count) 
					{
						form.DownButton.Enabled = true;
					}
					if (index > 0) 
					{
						form.UpButton.Enabled = true;
					}
				} 
				else 
				{
					form.DownButton.Enabled = false;
					form.UpButton.Enabled = false;
				}
			}
		}

		public virtual void HandleEditItemEvent() 
		{
			Key key = GetItemKey(listView.FocusedItem);
			ItemBase item = list.GetItem(key);

			if (item != null) 
			{
				int index = listView.FocusedItem.Index;
				IItemPropertiesForm propertiesForm = form.CreatePropertiesForm(item);
				if (((Form)propertiesForm).ShowDialog() != DialogResult.Cancel) 
				{
					item = propertiesForm.GetItem(EditMode.Modify);
					if (item != null)
					{
						RefreshIndexedItem(index, item);
					}
				}
			}
		}

		virtual public void ToolbarEventsHandler(ToolBarButtonClickEventArgs e)
		{
			if (e.Button == form.DeleteButton) 
			{
				if (MessageBox.Show(LangResources.Instance.Resources.GetString("deleteConfirm"),
					LangResources.Instance.Resources.GetString("applName"),
					MessageBoxButtons.YesNo,
					MessageBoxIcon.Question,
					MessageBoxDefaultButton.Button2) == DialogResult.Yes) 
				{

					DeleteItem(listView.FocusedItem);
                    
					EnableDisableItems();
				}
				return;
			}
			if (e.Button == form.NewButton) 
			{
				IItemPropertiesForm propertiesForm = form.CreatePropertiesForm(null);

				SetDefaultValue(propertiesForm);
				
				if (((Form)propertiesForm).ShowDialog() != DialogResult.Cancel) 
				{
					ItemBase newItem = propertiesForm.GetItem(EditMode.Add);
					if (newItem != null) 
					{
						AddItem(newItem);
						EnableDisableItems();
					}
					return;
				}
			}
			if (e.Button == form.EditButton) 
			{				
				HandleEditItemEvent();
			}
			if (e.Button == form.UpButton) 
			{
				UpItem(listView.FocusedItem);
			}
			if (e.Button == form.DownButton) 
			{
				DownItem(listView.FocusedItem);
			}
		}

		virtual public void SetDefaultValue(IItemPropertiesForm form) 
		{
		}
	}	
}
